package com.zsh.wx_account1.controller;

import com.zsh.wx_account1.entity.*;
import com.zsh.wx_account1.util.HttpUtil;
import com.zsh.wx_account1.util.TokenUtil;
import net.sf.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 *  二级菜单
 * @Author ZhaoShuHao
 * @Date 2023/8/23 9:37
 */
public class TestButton {

    public static void main(String[] args) {
        //测试号
        String APP_ID = "wx43f47bd394a23feb";
        String url1="https://blog.csdn.net/ZhShH0413?spm=1010.2135.3001.5343";
        String encode = URLEncoder.encode(url1);
        String url2="https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        String formatUrl = String.format(url2, APP_ID, encode);
        System.out.println(formatUrl);
        //创建一级菜单
        Button button = new Button();
        List<AbstractButton> buttons = new ArrayList<>();
        //一级菜单中的第一个按钮
        ClickButton clickButton = new ClickButton("笑话大全","jokes");
        //一级菜单中的第二个按钮
        ClickButton clickButton2 = new ClickButton("谜语大全","rddle");
        //一级菜单中的第三个按钮（二级菜单）
        SubButton subButton = new SubButton("快快点我");
        List<AbstractButton> subButtons = new ArrayList<>();
        //二级菜单的第一个按钮
//        subButtons.add(new ViewButton("大佬给个关注吧","https://blog.csdn.net/ZhShH0413?spm=1010.2135.3001.5343"));
        subButtons.add(new ViewButton("大佬给个关注吧",formatUrl));
        //二级菜单的第二个按钮
        subButtons.add(new ClickButton("心灵鸡汤","soup"));
        //二级菜单的第二个按钮
        subButtons.add(new PhotoOrAlbumButton("上传图片(带有文字的图片有惊喜哦)"));
        subButton.setSub_button(subButtons);
        //把一级菜单中的三个按钮添加进集合
        buttons.add(clickButton);
        buttons.add(clickButton2);
        buttons.add(subButton);
        //把集合添加到一级菜单中
        button.setButton(buttons);
        //转换成json字符串
        JSONObject jsonObject = JSONObject.fromObject(button);
        System.out.println(jsonObject);
        String json = jsonObject.toString();
        String url = String.format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", TokenUtil.getAccessToken());
        //发送请求
        String result = HttpUtil.doPost(url, json,"application/json;charset=utf-8");
        System.out.println(result);

    }

}
